<template>
  <div>
    <el-dialog
      title="积分充值入口（积分日元1：1）"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      width="600px"
      :before-close="cancel"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="充值点数" prop="orderAmount">
          <el-input type="number" v-model.number="form.orderAmount" />
        </el-form-item>
        <el-form-item label="充值理由" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="7" show-word-limit maxlength="500" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-hasPermi="['distributor:distributorPoints:sellerPointsPayInfo']" @click="handlePay">
          支付
        </el-button>
      </div>
    </el-dialog>
    <Pay
      :filter="filter"
      :show.sync="pay.show"
      @getList="getList"
      @updataPayType="updataPayType"
      :useDefaultDetail="true"
      :formData="pay.formData"
    >
      <template v-hasPermi="['distributor:distributorPoints:sellerPointsPayOffline']" #default="{ handlePayMethod }">
        <div style="margin: 20px 0">线下支付:</div>
        <div class="payContentOffline">
          <el-button
            :class="{ 'is-active': offlinePayTypestr == 1 }"
            @click="offlinePayType(1, handlePayMethod, 'offlineCash')"
            >现金支付</el-button
          >
          <el-button
            :class="{ 'is-active': offlinePayTypestr == 2 }"
            @click="offlinePayType(2, handlePayMethod, 'offlineTransfer')"
            >银行卡转账</el-button
          >
        </div>
      </template>
      <!-- <template #detail="{ detail }">
        <div class="payMessage">
          <el-row :gutter="20">
            <el-col :span="12">{{ $t('订单数量') }} :{{ detail.payBillNum }}</el-col>
            <el-col :span="12">
              {{ $t('金额') }} :{{ detail.orderAmount ? (+detail.orderAmount).toFixed() : '' }}JPY
            </el-col>
          </el-row>
        </div>
      </template> -->
    </Pay>
  </div>
</template>

<script>
import { sellerPointsPayInfo, sellerPointsPayOffline } from '@/api/bill/sellerPoints'
import Pay from '@/components/Pay/index.vue'
export default {
  components: {
    Pay,
  },
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * data
     */
    data: {
      type: Array,
      default: () => [],
    },
    /**
     * 标题
     */
    title: {
      type: String,
      required: true,
    },
    /**
     * type
     */
    type: {
      type: [String, Number],
      required: true,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      offlinePayTypestr: '', //支付方式
      pay: {
        show: false,
        type: '',
        formData: {},
        idList: [],
      },
      queryParams: {},
      form: {},
      // 表单校验
      rules: {
        remark: [{ required: true, message: '请输入', trigger: 'blur' }],
        orderAmount: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
      }
    },
  },
  methods: {
    filter(data) {
      // 筛选积分支付
      const filterData = data.filter((i) => i.payChannel != 'pointPay')
      return filterData
    },
    updataPayType(selectPayChannel) {
      if (selectPayChannel) {
        this.offlinePayTypestr = selectPayChannel == 'offlineCash' ? 1 : 2
        const data = {
          id: this.pay.formData.businessId,
          offlinePayType: this.offlinePayTypestr,
          remark: this.form.remark,
        }
        this.$modal
          .confirm(
            `是否确认${this.offlinePayTypestr == 1 ? '现金支付' : this.offlinePayTypestr == 2 ? '银行卡转账' : ''}支付`,
          )

          .then(() => {
            sellerPointsPayOffline(data).then((res) => {
              this.$message({ type: 'success', message: '操作成功' })
              this.pay.show = false
              this.getList()
            })
          })
      } else {
        this.offlinePayTypestr = ''
      }
    },
    /**线下支付方式 */
    offlinePayType(offlinePayType, handlePayMethod, payTypeStr) {
      this.offlinePayTypestr = offlinePayType
      handlePayMethod({ payChannel: payTypeStr })
    },
    getList() {
      this.$emit('getList')
      this.cancel()
    },
    handlePay() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          const data = { amount: this.form.orderAmount, remark: this.form.remark }
          sellerPointsPayInfo(data).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.pay.formData = {
                ...data,
              }
              this.pay.show = true
            }
          })
        }
      })
    },

    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss">
.payMessage {
  text-align: center;
  font-size: 16px;
  color: black;
  font-weight: 700;
  margin-bottom: 10px;
}
</style>
